<?php
/* --------------------------------------------------------------
   ProductPropertiesData.php 2009-12-14 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2009 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/


class ProductPropertiesData 
{
	var $v_products_id = 0;
	var $v_language_id = 0;
	
	var $v_properties_struct 		= array();
	var $v_properties_combis_array 	= array();
	
	
	function ProductPropertiesData($p_products_id, $p_language_id) 
	{
		$this->v_products_id = (int)$p_products_id;
		$this->v_language_id = (int)$p_language_id;
		
		$this->init_properties_struct($this->v_products_id, $this->v_language_id);
	}
	
	function init_properties_struct($p_products_id, $p_language_id)
	{
		$c_products_id = (int)$p_products_id;
		$c_language_id = (int)$p_language_id;
		
		$t_properties_array 		= array();
		$t_properties_combis_array 	= array();
		
		
		# select properties_name for grouping properties_values
		$t_sql = '
			SELECT
				properties_id,
				properties_name
			FROM 
				products_properties_index
			WHERE
				products_id = "'.$c_products_id.'" AND
				language_id = "'.$c_language_id.'"
			GROUP BY 
				properties_id
			ORDER BY
				properties_sort_order,
				properties_id

		';
		$t_result = xtc_db_query($t_sql);
		
		# init properties_names
		while(($t_row = xtc_db_fetch_array($t_result) ))
		{
			$t_properties_array[$t_row['properties_id']] = array(
															'properties_id'		=> $t_row['properties_id'],
															'properties_name' 	=> $t_row['properties_name'],
															'values_array'		=> array()
														);
		}

		
		# select properties_combis for grouping properties_values
		$t_sql = '
			SELECT
				ppi.products_properties_combis_id
			FROM
				products_properties_combis AS ppc LEFT JOIN products_properties_index AS ppi USING (products_properties_combis_id)
			WHERE
				ppi.products_id = "'.$c_products_id.'" AND
				ppi.language_id = "'.$c_language_id.'"
			GROUP BY 
				products_properties_combis_id
			ORDER BY
				ppc.sort_order
		';
		$t_result = xtc_db_query($t_sql);
		
		# init properties_combis
		while(($t_row = xtc_db_fetch_array($t_result) ))
		{
			$t_properties_combis_array[$t_row['products_properties_combis_id']] = array(
																					'products_properties_combis_id' => $t_row['products_properties_combis_id'],
																					'values_ids_array'				=> array()
																				);
		}
		
		
		# select properties_values and dispatch
		$t_sql = '
			SELECT *
			FROM 
				products_properties_index
			WHERE
				products_id = "'.$c_products_id.'" AND
				language_id = "'.$c_language_id.'"
	--		GROUP BY 
	--			properties_values_id
			ORDER BY
				value_sort_order,
				properties_values_id
		';
		$t_result = xtc_db_query($t_sql);
		
		# init properties_values and group in properties
		while(($t_row = xtc_db_fetch_array($t_result) ))
		{
			$xtPrice = new xtcPrice($_SESSION['currency'], $_SESSION['customers_status']['customers_status_id']);
                        
                        $coo_data_group = MainFactory::create_object('GMDataObjectGroup', array('properties_values', array('properties_values_id' => $t_row['properties_values_id']) ));
                        $t_data_object_array = $coo_data_group->get_data_objects_array(); 
                        
			$properties_value_price = $xtPrice->xtcCalculateCurr($t_data_object_array[0]->get_data_value('value_price'));
			
			$t_properties_array[$t_row['properties_id']]['values_array'][$t_row['properties_values_id']] = array(
                                                                                                                                    'properties_values_id' => $t_row['properties_values_id'],
                                                                                                                                    'values_name' => $t_row['values_name'],
                                                                                                                                    'values_price' => number_format($properties_value_price, 2)
                                                                                                                            );
			$t_properties_combis_array[$t_row['products_properties_combis_id']]['values_ids_array'][] = $t_row['properties_values_id'];
		}
		$this->v_properties_struct = $t_properties_array;
		$this->v_properties_combis_array = $t_properties_combis_array;

		//print_r($t_properties_array);
	}
	
	function get_properties_struct()
	{
		return $this->v_properties_struct;
	}
	
	function get_properties_combis_array()
	{
		return $this->v_properties_combis_array;
	}
}

?>